home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / ScreenSavers / bundle / Source / BouncingView.m < prev    next >
Text File  |  1995-06-12  |  3KB  |  149 lines

  1.  
  2. #import "BouncingView.h"
  3.  
  4. @implementation BouncingView
  5.  
  6. -    init
  7. {    
  8.     const NXScreen    *mainScreen;
  9.     int            windowNum;
  10.     NXRect        windowRect;
  11.         
  12.     mainScreen = [NXApp mainScreen];
  13.     myWindow = [[Window alloc] initContent: &(mainScreen->screenBounds)
  14.         style: NX_PLAINSTYLE backing: NX_BUFFERED buttonMask: 0
  15.         defer: YES];
  16.     windowNum = [myWindow windowNum];
  17.     [myWindow setBackgroundGray: NX_DKGRAY];
  18.     PSsetwindowlevel( 50, windowNum );
  19.     [[myWindow contentView] getBounds: &windowRect];
  20.     [self initFrame: &windowRect];
  21.     [[myWindow contentView] addSubview: self];
  22.     xDelta = yDelta = DELTA;
  23.     currentPoint.x = (frame.size.width/2) - (imageSize.width/2);
  24.     currentPoint.y = (frame.size.height/2) - (imageSize.height/2);
  25.     previousRect.origin = currentPoint;
  26.     previousRect.size = imageSize;
  27.     
  28.     return self;
  29. }
  30.  
  31. -    drawSelf: (const NXRect *)rects :(int)rectCount
  32. {
  33.     [background composite:NX_SOVER fromRect:&previousRect
  34.         toPoint:&previousRect.origin];
  35.     [slidingImage composite: NX_SOVER toPoint: ¤tPoint];
  36.     
  37.     return self;
  38. }
  39.  
  40. -    initFrame: (const NXRect *)frameRect
  41. {
  42.     NXRect    initRect;
  43.     const char     *imageFile;
  44.     
  45.     [super initFrame: frameRect];
  46.     
  47.     [self allocateGState];
  48.     imageFile = NXReadDefault( [NXApp name], "ImageFile" );
  49.     if( imageFile && *imageFile )
  50.     {
  51.         NXRunAlertPanel( "BV", "opening: %s", NULL, NULL, NULL, imageFile );
  52.         slidingImage = [[NXImage allocFromZone: [self zone]] 
  53.             initFromFile: imageFile];
  54.     }
  55.     else
  56.     {
  57.         slidingImage = [[NXImage allocFromZone: [self zone]] 
  58.         initFromFile: "/usr/lib/NextStep/loginwindow.app/LoginPanel.tiff"];
  59.     }
  60.     [slidingImage getSize: &imageSize];
  61.     if( slidingImage )
  62.         [slidingImage free];
  63.     initRect.origin.x =    initRect.origin.y = 0.0;
  64.     initRect.size = imageSize;
  65.     [[[NXApp mainWindow] contentView] lockFocus];
  66.     slidingImageRep = [[NXBitmapImageRep allocFromZone: [self zone]]
  67.         initData: NULL fromRect: (const NXRect *)&initRect];
  68.     [[[NXApp mainWindow] contentView] unlockFocus];
  69.     slidingImage = [[NXImage allocFromZone: [self zone]] 
  70.         initSize: &imageSize];
  71.     [slidingImage useRepresentation: slidingImageRep];
  72.     PSsetgray( NX_DKGRAY );
  73.     [(background = [[NXImage allocFromZone:[self zone]] init])
  74.         setScalable:NO];
  75.     [background useDrawMethod:@selector(drawDefaultBackground:)
  76.         inObject:self];
  77.     [background setSize: &(bounds.size)];
  78.     
  79.     return self;
  80. }
  81.  
  82. -     drawDefaultBackground:imageRep
  83. {
  84.     [background setSize: &(bounds.size)];
  85.     PSsetgray( NX_DKGRAY );
  86.     NXRectFill( &bounds );
  87.  
  88.     return self;
  89. }
  90.  
  91. -    startScreenSaver
  92. {
  93.     [myWindow orderFront: self];
  94.     [myWindow display];
  95.     
  96.     return self;
  97. }
  98.  
  99. -    stopScreenSaver
  100. {
  101.     [myWindow orderOut: self];
  102.     
  103.     return self;
  104. }
  105.  
  106. -    free
  107. {
  108.     [super free];
  109.     [myWindow free];
  110.     [slidingImage free];
  111.     [background free];
  112.     
  113.     return self;
  114. }
  115.  
  116. -    step
  117. {
  118.     previousRect.origin = currentPoint;
  119.     currentPoint.x += xDelta;
  120.     currentPoint.y += yDelta;
  121.     
  122.     if( currentPoint.x > (frame.size.width - imageSize.width) )
  123.     {
  124.         currentPoint.x = frame.size.width - imageSize.width;
  125.         xDelta = -xDelta;
  126.     }
  127.     else if( currentPoint.x < 0 )
  128.     {
  129.         currentPoint.x = 0;
  130.         xDelta = -xDelta;
  131.     }
  132.     if( currentPoint.y > (frame.size.height - imageSize.height) )
  133.     {
  134.         currentPoint.y = frame.size.height - imageSize.height;
  135.         yDelta = -yDelta;
  136.     }
  137.     else if( currentPoint.y < 0 )
  138.     {
  139.         currentPoint.y = 0;
  140.         yDelta = -yDelta;
  141.     }
  142.     [self display];
  143.     [window flushWindow];
  144.     
  145.     return self;
  146. }
  147.  
  148. @end
  149.